using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using dender.dvdcover.core;
using System.Drawing;
using dender.dvdcover.core.image;

namespace dender.dvdcover.gui.custom
{
    /// <summary>
    /// Defines a Picturebox with a part reference
    /// </summary>
    public class PartPicturebox : PictureBox
    {
        private Part mPart;
        /// <summary>
        /// Gets or Sets the part of the PartPictureBox
        /// </summary>
        public Part Part
        {
            get { return mPart; }
            set { mPart = value; }
        }

        public PartPicturebox(ContextMenuStrip menu, Part part) : base()
        {
            this.BorderStyle = BorderStyle.None;
            this.ContextMenuStrip = menu;
            this.Part = part;
        }
        
        /// <summary>
        /// Loads an image into the PictureBox
        /// </summary>
        /// <param name="image"></param>
        public void LoadImage(Bitmap image)
        {
            CoverImage coverImage = new CoverImage(this.Part,image);
            coverImage.OnImageChanged +=new EventHandler<ImageChangedEventArgs>(image_OnImageChanged);
            mPart.Fill(coverImage);
            this.Refresh();
        }

        public override void Refresh()
        {
            this.Image = this.Part.Image.Image;
            base.Refresh();
        }

        void image_OnImageChanged(object sender, ImageChangedEventArgs e)
        {
            this.Image = e.NewImage;
            this.Refresh();
        }

        /// <summary>
        /// Checks if the PictureBox contains an image
        /// </summary>
        /// <returns></returns>
        public bool HasImage
        {
            get
            {
                return (this.Part.Image != null);
            }
        }

        public void CenterResize()
        {
            if (this.HasImage)
            {
                this.Part.Image.Resize(new CenterResizeStyle());
            }
        }
        public void StrechResize()
        {
            if (this.HasImage)
            {
                this.Part.Image.Resize(new StretchResize());
            }
        }

        public void ResetTransforms()
        {
            if (this.HasImage)
            {
                this.Part.Image.ResetTransform();
            }
        }
    }
}
